package com.haier.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.haier.constant.HpConstant;
import com.haier.utils.EmptyUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haier.dto.HpAreaGoTrackDto;
import com.haier.entity.HpAreaGoTrack;
import com.haier.mapper.HpAreaGoTrackHistoryMapper;
import com.haier.mapper.HpAreaGoTrackMapper;
import com.haier.mapper.HpDayMapper;
import com.haier.mapper.HpHoursMapper;
import com.haier.mapper.HpMonthMapper;
import com.haier.service.IHpAreaGoTrackService;
import com.haier.utils.GetUtils;
import com.haier.vo.HpAreaGoTrackVo;

/**
 * <p>
 * 通行轨迹 服务实现类
 * </p>
 *
 * @author luanxuecheng
 * @since 2020-07-01
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class HpAreaGoTrackServiceImpl extends ServiceImpl<HpAreaGoTrackMapper, HpAreaGoTrack>
		implements IHpAreaGoTrackService {
	@Autowired
	private HpAreaGoTrackHistoryMapper hpAreaGoTrackHistoryMapper;
	@Autowired
	private HpHoursMapper hpHoursMapper;
	@Autowired
	private HpDayMapper hpDayMapper;
	@Autowired
	private HpMonthMapper hpMonthMapper;

	@SuppressWarnings("rawtypes")
	@Override
	public IPage page(HpAreaGoTrackDto dto) {
		Page<HpAreaGoTrackVo> page = new Page<>(dto.getPageNo(), dto.getPageSize(), dto.getHasTotal());
		IPage<HpAreaGoTrackVo> iPage = baseMapper.pageByReport(page, dto);
		return iPage;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public IPage pageHistory(HpAreaGoTrackDto dto) {
		Page<HpAreaGoTrackVo> page = new Page<>(dto.getPageNo(), dto.getPageSize(), dto.getHasTotal());
		IPage<HpAreaGoTrackVo> iPage = hpAreaGoTrackHistoryMapper.pageByReport(page, dto);
		return iPage;
	}

	@Override
	public List<Map<String, Object>> reportTotayHoursCount(HpAreaGoTrackDto dto) {
		List<Map<String, Object>> list = hpHoursMapper.reportTotayHoursCount(dto);
		return list;
	}

	@Override
	public List<Map<String, Object>> reportWeekDayCount(HpAreaGoTrackDto dto) {
		Date today = new Date();
		String t = GetUtils.getDateString(today, "yyyy-MM-dd");
		dto.setStartTime(t);
		dto.setEndTime(t);
		dto.setIsSuccess(true);
		Long count = baseMapper.countByDto(dto);
		Map<String, Object> todayMap = new HashMap<>();
		todayMap.put("time", t);
		todayMap.put("count", count);

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		Date yesterday = cal.getTime();
		String y = GetUtils.getDateString(yesterday, "yyyy-MM-dd");
		dto.setEndTime(y);
		cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -6);
		Date lastWeekDay = cal.getTime();
		String l = GetUtils.getDateString(lastWeekDay, "yyyy-MM-dd");
		dto.setStartTime(l);
		List<Map<String, Object>> list = hpDayMapper.reportDayCount(dto);

		list.add(todayMap);

		return list;
	}

	/**
	 *
	 * @param dto
	 * @return
	 */
	@Override
	public SXSSFWorkbook reportTrack(HpAreaGoTrackDto dto) {
		//查询申请列表
		List<HpAreaGoTrackVo> list = baseMapper.pageByReport(dto);

		SXSSFWorkbook book = new SXSSFWorkbook();
		Sheet sheet = book.createSheet("申请列表");
		Row row = sheet.createRow(0);
		CellStyle style = book.createCellStyle();
		Font font = book.createFont();
		font.setFontName("宋体");
		font.setBoldweight(Font.BOLDWEIGHT_BOLD);// 粗体
		style.setFont(font);
		Cell cell = row.createCell(0);
		row.createCell(0).setCellValue("工号");
		row.createCell(1).setCellValue("姓名");
		row.createCell(2).setCellValue("园区");
		row.createCell(3).setCellValue("区域");
		row.createCell(4).setCellValue("时间");
		for (int i = 0; i < 5; i++) {
			cell = row.getCell(i);
			cell.setCellStyle(style);
		}

		if (EmptyUtils.isEmpty(list)) {
			return book;
		}
		Iterator<HpAreaGoTrackVo> it = list.iterator();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int i = 1;
		while (it.hasNext()) {
			HpAreaGoTrackVo hpAreaGoTrackVo = it.next();
			row = sheet.createRow(i++);
			row.createCell(0).setCellValue(GetUtils.getString(hpAreaGoTrackVo.getJobNumber()));//工号
			row.createCell(1).setCellValue(GetUtils.getString(hpAreaGoTrackVo.getName()));//姓名
			row.createCell(2).setCellValue(GetUtils.getString(hpAreaGoTrackVo.getGarden()));//园区
			row.createCell(3).setCellValue(GetUtils.getString(hpAreaGoTrackVo.getArea()));//区域
			String dateString = formatter.format(hpAreaGoTrackVo.getTime());
			row.createCell(4).setCellValue(dateString);//创建时间
		}
		return book;
	}

	/**
	 * 查询轨迹列表
	 * @param dto
	 * @return
	 */
	@Override
	public List<HpAreaGoTrackVo> getListReport(HpAreaGoTrackDto dto) {
		List<HpAreaGoTrackVo> list = baseMapper.pageByReport(dto);
		return list;
	}

	@Override
	public List<Map<String, Object>> reportHoursCount(HpAreaGoTrackDto dto) {
		List<Map<String, Object>> list = hpHoursMapper.reportHoursCount(dto);
		return list;
	}

	@Override
	public List<Map<String, Object>> reportDayCount(HpAreaGoTrackDto dto) {
		List<Map<String, Object>> list = hpDayMapper.reportDayCount(dto);
		return list;
	}

	@Override
	public List<Map<String, Object>> reportMonthCount(HpAreaGoTrackDto dto) {
		List<Map<String, Object>> list = hpMonthMapper.reportDayCount(dto);
		return list;
	}

}
